package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ewms.bo.EwmsWasteInventoryAddBo;
import ewms.bo.EwmsWasteInventoryEditBo;
import ewms.bo.EwmsWasteInventoryQueryBo;
import ewms.common.core.domain.FileEntity;
import ewms.common.utils.PageUtils;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.service.IEwmsWasteInventoryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ewms.domain.EwmsWasteInventory;
import ewms.mapper.EwmsWasteInventoryMapper;
import ewms.vo.EwmsWasteInventoryVo;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Optional;

/**
 * 危废库存信息Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-02-09
 */
@Service
public class EwmsWasteInventoryServiceImpl extends ServiceImpl<EwmsWasteInventoryMapper, EwmsWasteInventory> implements IEwmsWasteInventoryService {

	@Override
	public EwmsWasteInventoryVo queryById(Long wasteInventoryId) {
		return getVoById(wasteInventoryId, EwmsWasteInventoryVo.class);
	}

	@Override
	public TableDataInfo<EwmsWasteInventoryVo> selectPageWasteInventoryList(EwmsWasteInventoryQueryBo bo) {
		Page<EwmsWasteInventoryVo> page = baseMapper.selectPageWasteInventoryList(PageUtils.buildPage(), bo);
		page.getRecords().stream().forEach(vo -> Optional.ofNullable(vo.getAttachment())
			.ifPresent(attachment -> vo.setFileList(JSON.parseObject(attachment, new TypeReference<List<FileEntity>>() {
			}.getType())))
		);
		return PageUtils.buildDataInfo(page);
	}

	@Override
	public List<EwmsWasteInventoryVo> selectWasteInventoryList(EwmsWasteInventoryQueryBo bo) {
		return baseMapper.selectWasteInventoryList(bo);
	}


	@Override
	public TableDataInfo<EwmsWasteInventoryVo> queryPageList(EwmsWasteInventoryQueryBo bo) {
		PagePlus<EwmsWasteInventory, EwmsWasteInventoryVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsWasteInventoryVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<EwmsWasteInventoryVo> queryList(EwmsWasteInventoryQueryBo bo) {
		return listVo(buildQueryWrapper(bo), EwmsWasteInventoryVo.class);
	}

	private LambdaQueryWrapper<EwmsWasteInventory> buildQueryWrapper(EwmsWasteInventoryQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<EwmsWasteInventory> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getWasteId() != null, EwmsWasteInventory::getWasteId, bo.getWasteId());
		lqw.eq(bo.getWarehouseId() != null, EwmsWasteInventory::getWarehouseId, bo.getWarehouseId());
		lqw.like(StrUtil.isNotBlank(bo.getWasteName()), EwmsWasteInventory::getWasteName, bo.getWasteName());
		lqw.like(StrUtil.isNotBlank(bo.getBatchNumber()), EwmsWasteInventory::getBatchNumber, bo.getBatchNumber());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(EwmsWasteInventoryAddBo bo) {
		EwmsWasteInventory add = BeanUtil.toBean(bo, EwmsWasteInventory.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(EwmsWasteInventoryEditBo bo) {
		EwmsWasteInventory update = BeanUtil.toBean(bo, EwmsWasteInventory.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(EwmsWasteInventory entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
